package com.engine4cloud.tdf.supervision.controller.web;

import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.create.JzdxJudgmentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxJudgmentResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxJudgmentUpdateDTO;
import com.engine4cloud.tdf.supervision.service.JzdxJudgmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * 矫正对象-判决信息-web
 *
 * @author wenjie
 * @date 2024-03-15 15:08:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/judgment")
@Tag(description = "JudgmentWeb", name = "矫正对象-判决信息-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class JzdxJudgmentWebController {

    private final JzdxJudgmentService jzdxJudgmentService;

    /**
     * 分页查询
     *
     * @param code 矫正对象编号
     * @return R<JzdxJudgmentResultDTO>
     */
    @Operation(summary = "根据矫正对象查询判决信息", description = "根据矫正对象查询判决信息")
    @GetMapping("/find")
    public R<JzdxJudgmentResultDTO> getCorrectorJudgmentPage(@Schema(description = "矫正对象编号") @ParameterObject String code) {
        JzdxJudgmentResultDTO result = jzdxJudgmentService.findByCode(code);
        return R.ok(Objects.isNull(result) ? new JzdxJudgmentResultDTO() : result);
    }

    /**
     * 新增矫正对象判决信息-新增对象
     *
     * @param dto 矫正对象判决信息-新增对象
     * @return R
     */
    @Operation(summary = "矫正对象判决信息-新增对象", description = "矫正对象判决信息-新增对象")
    @SysLog("矫正对象判决信息-新增对象")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('judgment_add')")
    public R<Boolean> save(@Valid @RequestBody JzdxJudgmentCreateDTO dto) {
        return R.ok(jzdxJudgmentService.create(dto));
    }

    /**
     * 修改矫正对象判决信息-更新对象
     *
     * @param dto 矫正对象判决信息-更新对象
     * @return R
     */
    @Operation(summary = "矫正对象判决信息-更新对象", description = "矫正对象判决信息-更新对象")
    @SysLog("矫正对象判决信息-更新对象")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('judgment_edit')")
    public R<Boolean> updateById(@Valid @RequestBody JzdxJudgmentUpdateDTO dto) {
        return R.ok(jzdxJudgmentService.update(dto));
    }
}